package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    @Override
    public Member createOrUpdate(Member member) {
        log.info("[会员添加]data:{}", member);
        Member pojo = memberDao.selectByPhoneNumber(member.getPhoneNumber());
        if (null != pojo) {
            //判断会员是否存在
            //存在
            member.setId(pojo.getId());
            memberDao.updateByIdSelective(member);
        } else {
            //不存在,添加数据
            member.setRegTime(new Date());
            memberDao.insert(member);
        }

        return memberDao.selectById(member.getId());
    }

    @Override
    public Member findById(Integer memberId) {
        return memberDao.selectById(memberId);
    }

}
